package algorithm;

import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by GodRUI on 2021/8/31 10:32
 *
 * 给定 nums = [2, 7, 11, 15], target = 9
 * 因为 nums[0] + nums[1] = 2 + 7 = 9
 * 所以返回 [0, 1]
 *
 */
public class TwoNums {

    public static void main(String[] args) {

        int[] nums = {1,2,4,6,8};
        int target = 8;

        System.out.println(Arrays.toString(getResult(nums, target)));


    }

    public static int[] getResult(int[] nums,int target){

        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int x = target - nums[i];

            if (map.containsKey(x)){
                return new int[]{map.get(x),i};
            }

            map.put(nums[i],i);

        }

        return null;
    }
}
